package com.neuq.animaladoption.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.neuq.animaladoption.mapper.ShelterMapper;
import com.neuq.animaladoption.pojo.Animal;
import com.neuq.animaladoption.pojo.PageBean;
import com.neuq.animaladoption.pojo.Shelter;
import com.neuq.animaladoption.service.ShelterService;
import com.neuq.animaladoption.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ShelterServiceImpl implements ShelterService {

    @Autowired
    private ShelterMapper shelterMapper;

    @Override
    public void add(Shelter shelter) {
        shelter.setCreateTime(LocalDateTime.now());
        shelter.setUpdateTime(LocalDateTime.now());
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");
        shelter.setCreateUser(userid);
        shelterMapper.add(shelter);
    }

    @Override
    public Shelter findById(Integer id) {
        return shelterMapper.findByID(id);
    }

    @Override
    public void update(Shelter shelter) {
        shelter.setUpdateTime(LocalDateTime.now());
        shelterMapper.update(shelter);
    }

    @Override
    public void deleteById(Integer id) {
        shelterMapper.deleteById(id);
    }

    @Override
    public PageBean<Shelter> list(Integer pageNum, Integer pageSize,String cityAddress) {
        PageBean<Shelter> pb = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Shelter> ss = shelterMapper.list(cityAddress);
        Page<Shelter> p = (Page<Shelter>) ss;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

}
